<?php
include '../include/functions.php';
//set session save path
session_save_path($session_path);
// Initialize a session. 
session_start();
$count++;
$sessionId = session_id();
if((!$userID) || (!$start))
{
	header("Location: schedule.php"); /* Redirect browser */
	/* Make sure that code below does not get executed when we redirect. */
	exit;
}
//require_once('../include/QuickPicks.php');

function doInsert($userID,$Week, $TeamPick, $GamePick)
{
	//echo "Inserting - $userID, $Week, '$TeamPick', $GamePick...<br/>";
	$SQL = "INSERT INTO NHLSeriesPicks (UserID, Week, Team, NumGames) VALUES($userID, $Week, '$TeamPick', $GamePick)";
	mysql_query($SQL);
	LogPick($Week,$SQL);

}
function doUpdate($userID,$Week, $TeamPick, $GamePick)
{
	//echo "Updating - $userID,$Week, $TeamPick, $GamePick...<br/>";
	$SQL = "UPDATE NHLSeriesPicks SET Team='$TeamPick', NumGames=$GamePick WHERE UserID=$userID AND Week=$Week";
	mysql_query($SQL);
	LogPick($Week,$SQL);
}

function LogPick($Week, $SQL)
{
	$filename = "notes/week".$Week.".txt";
	$fp = fopen($filename,"a");
	fputs($fp,date('m-d-Y H:i:s')." - ".$SQL."\n");
	fclose($fp);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
    <title>JoshHughes.com NHL Hockey Pool</title>
	<link rel="shortcut icon" href="http://hockey.joshhughes.com/NHL/images/nhlFavicon.gif" />
	<link rel="stylesheet" href="http://hockey.joshhughes.com/css/NHL.css" type="text/css">
	<script src="http://hockey.joshhughes.com/scripts/prototype.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/scriptaculous.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/hockey.js" type="text/javascript"></script>
  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<!-- Main Contents -->

			<?php			  
				if((time() - $start)  > 1200)//if session is more than 20 mins old
				{	//logout: DO NOT SUBMIT PICKS!!!
				?>0
					<SCRIPT language="JavaScript">
						<!--0
						window.location="http://hockey.joshhughes.com/site-wide/logoff.php?timeout=true$League=NHL";
						//-->
					</SCRIPT>
				<?
				}
				else
				{
					if ($userID)
					{
						//Connect To Database
						dbConnect();
						
						$CanChangePick = array_fill(1, 15, true);
						$InsertPick = array_fill(1, 15, true);
						
						for ($Num = 1; $Num < 16; $Num++)
						{
							$sql_string = "SELECT Started FROM NHLSeries WHERE Week = $Num";
							$result = mysql_query($sql_string) or die("Cannot query the database.<br/>");
							$myrow = mysql_fetch_array($result);
							if ($myrow["Started"] != 0)
								$CanChangePick[$Num] = false;
								
							$sql_string = "SELECT Team FROM NHLSeriesPicks WHERE Week = $Num AND UserID = $userID";
							$result = mysql_query($sql_string) or die("Cannot query the database.<br/>");
							$myrow = mysql_fetch_array($result);
							if (($myrow["Team"] != "") || ($myrow["Team"] != NULL))
								$InsertPick[$Num] = false;
							
						}
						//Set DoQuery flag to false, if no pick were passed there is no need to query
						$doQuery = false;						
						
						echo "<H1>Your Series Picks</H1>";										
			// ________________  INSERTS -------------------
						if (($Series1 != "") && ($CanChangePick[1] == true) && ($InsertPick[1] == true)) {doInsert($userID,1,$Series1,$Series1Games);}
						if (($Series2 != "") && ($CanChangePick[2] == true) && ($InsertPick[2] == true)) {doInsert($userID,2,$Series2,$Series2Games);}
						if (($Series3 != "") && ($CanChangePick[3] == true) && ($InsertPick[3] == true)) {doInsert($userID,3,$Series3,$Series3Games);}
						if (($Series4 != "") && ($CanChangePick[4] == true) && ($InsertPick[4] == true)) {doInsert($userID,4,$Series4,$Series4Games);}
						if (($Series5 != "") && ($CanChangePick[5] == true) && ($InsertPick[5] == true)) {doInsert($userID,5,$Series5,$Series5Games);}
						if (($Series6 != "") && ($CanChangePick[6] == true) && ($InsertPick[6] == true)) {doInsert($userID,6,$Series6,$Series6Games);}
						if (($Series7 != "") && ($CanChangePick[7] == true) && ($InsertPick[7] == true)) {doInsert($userID,7,$Series7,$Series7Games);}
						if (($Series8 != "") && ($CanChangePick[8] == true) && ($InsertPick[8] == true)) {doInsert($userID,8,$Series8,$Series8Games);}
						if (($Series9 != "") && ($CanChangePick[9] == true) && ($InsertPick[9] == true)) {doInsert($userID,9,$Series9,$Series9Games);}
						if (($Series10 != "") && ($CanChangePick[10] == true) && ($InsertPick[10] == true)) {doInsert($userID,10,$Series10,$Series10Games);}
						if (($Series11 != "") && ($CanChangePick[11] == true) && ($InsertPick[11] == true)) {doInsert($userID,11,$Series11,$Series11Games);}
						if (($Series12 != "") && ($CanChangePick[12] == true) && ($InsertPick[12] == true)) {doInsert($userID,12,$Series12,$Series12Games);}
						if (($Series13 != "") && ($CanChangePick[13] == true) && ($InsertPick[13] == true)) {doInsert($userID,13,$Series13,$Series13Games);}
						if (($Series14 != "") && ($CanChangePick[14] == true) && ($InsertPick[14] == true)) {doInsert($userID,14,$Series14,$Series14Games);}
						if (($Series15 != "") && ($CanChangePick[15] == true) && ($InsertPick[15] == true)) {doInsert($userID,15,$Series15,$Series15Games);}
			// ________________  UPDATES -------------------
						if (($Series1 != "") && ($CanChangePick[1] == true) && ($InsertPick[1] != true)) {doUpdate($userID,1,$Series1,$Series1Games);}
						if (($Series2 != "") && ($CanChangePick[2] == true) && ($InsertPick[2] != true)) {doUpdate($userID,2,$Series2,$Series2Games);}
						if (($Series3 != "") && ($CanChangePick[3] == true) && ($InsertPick[3] != true)) {doUpdate($userID,3,$Series3,$Series3Games);}
						if (($Series4 != "") && ($CanChangePick[4] == true) && ($InsertPick[4] != true)) {doUpdate($userID,4,$Series4,$Series4Games);}
						if (($Series5 != "") && ($CanChangePick[5] == true) && ($InsertPick[5] != true)) {doUpdate($userID,5,$Series5,$Series5Games);}
						if (($Series6 != "") && ($CanChangePick[6] == true) && ($InsertPick[6] != true)) {doUpdate($userID,6,$Series6,$Series6Games);}
						if (($Series7 != "") && ($CanChangePick[7] == true) && ($InsertPick[7] != true)) {doUpdate($userID,7,$Series7,$Series7Games);}
						if (($Series8 != "") && ($CanChangePick[8] == true) && ($InsertPick[8] != true)) {doUpdate($userID,8,$Series8,$Series8Games);}
						if (($Series9 != "") && ($CanChangePick[9] == true) && ($InsertPick[9] != true)) {doUpdate($userID,9,$Series9,$Series9Games);}
						if (($Series10 != "") && ($CanChangePick[10] == true) && ($InsertPick[10] != true)) {doUpdate($userID,10,$Series10,$Series10Games);}
						if (($Series11 != "") && ($CanChangePick[11] == true) && ($InsertPick[11] != true)) {doUpdate($userID,11,$Series11,$Series11Games);}
						if (($Series12 != "") && ($CanChangePick[12] == true) && ($InsertPick[12] != true)) {doUpdate($userID,12,$Series12,$Series12Games);}
						if (($Series13 != "") && ($CanChangePick[13] == true) && ($InsertPick[13] != true)) {doUpdate($userID,13,$Series13,$Series13Games);}
						if (($Series14 != "") && ($CanChangePick[14] == true) && ($InsertPick[14] != true)) {doUpdate($userID,14,$Series14,$Series14Games);}
						if (($Series15 != "") && ($CanChangePick[15] == true) && ($InsertPick[15] != true)) {doUpdate($userID,15,$Series15,$Series15Games);}

						if (($Series1 != "") && ($CanChangePick[1] == true)) {echo "Series 1 pick = $Series1<br/>";}
						if (($Series2 != "") && ($CanChangePick[2] == true)) {echo "Series 2 pick = $Series2<br/>";}
						if (($Series3 != "") && ($CanChangePick[3] == true)) {echo "Series 3 pick = $Series3<br/>";}
						if (($Series4 != "") && ($CanChangePick[4] == true)) {echo "Series 4 pick = $Series4<br/>";}
						if (($Series5 != "") && ($CanChangePick[5] == true)) {echo "Series 5 pick = $Series5<br/>";}
						if (($Series6 != "") && ($CanChangePick[6] == true)) {echo "Series 6 pick = $Series6<br/>";}
						if (($Series7 != "") && ($CanChangePick[7] == true)) {echo "Series 7 pick = $Series7<br/>";}
						if (($Series8 != "") && ($CanChangePick[8] == true)) {echo "Series 8 pick = $Series8<br/>";}
						if (($Series9 != "") && ($CanChangePick[9] == true)) {echo "Series 9 pick = $Series9<br/>";}
						if (($Series10 != "") && ($CanChangePick[10] == true)) {echo "Series 10 pick = $Series10<br/>";}
						if (($Series11 != "") && ($CanChangePick[11] == true)) {echo "Series 11 pick = $Series11<br/>";}
						if (($Series12 != "") && ($CanChangePick[12] == true)) {echo "Series 12 pick = $Series12<br/>";}
						if (($Series13 != "") && ($CanChangePick[13] == true)) {echo "Series 13 pick = $Series13<br/>";}
						if (($Series14 != "") && ($CanChangePick[14] == true)) {echo "Series 14 pick = $Series14<br/>";}
						if (($Series15 != "") && ($CanChangePick[15] == true)) {echo "Series 15 pick = $Series15<br/>";}
						echo "<br/><a href=\"seriespicks.php\" class=\"permLink\">Go Back To &quot;Series Picks&quot; Page</a>";
					}
					else		//If no $userID
					{
						echo "<H2>You must be registered member of this pool site, and you must be logged in to submit picks.</H1><p></p>";
						echo "<p>If you are a registered member, and you are logged in, then you must not of made the playoffs!";
					}//end if ($UserID)
			}//end if (time() - $start) < 1200
	?>
			<!-- End Main Contents -->
			
			</div>
			<div id="leftcol" >
			
				<?PHP include_once("layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("layout/footer.html"); ?>
			
		</div>
</div>
</body>
</html>
